/**
 * @file sintp.c
 * @author zhuxuanlin (xuanlinzhu@qq.com)
 * @brief 实现一个lua mpy
 * @version 0.1
 * @date 2023-06-10
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include "sintp.h"
#include "sline.h"
#include "lua/lua.h"
#include "lua/lauxlib.h"
#include "lua/lualib.h"
#include <string.h>

#if(SL_OUTPUT_ENABLE)

static lua_State *L = NULL ;
int lua_deal_line(const char *str)
{
	int ret = luaL_dostring(L,str);
	if(ret != LUA_OK)
	{
		sl_printf("%s\r\n",lua_tostring(L ,-1));
		return -1;
	}	
	return 0;
}

// lua业务api
static int sl_output_api(char * str)
{
	if(strcmp(str ,"exit") == 0)
	{
		sl_output_t op = {-1,NULL};
		sl_output_set(&op);
		lua_close(L);
		sl_printf("	exit lua env!!!\r\n");
		return 0;
	}
	// 开始lua业务处理
	lua_deal_line(str);
	return 0;
}

int myf(lua_State *L)
{
	int a = luaL_checknumber(L,1);
	int b = luaL_checknumber(L,2);
	sl_printf("mtf:%d\r\n",a*b);
	lua_pushnumber(L,a*b);
	return 1;
}
// 开启lua环境
int lua_enable(int argc ,char * argv[])
{
	sl_output_t op = {0,sl_output_api};
	sl_output_set(&op);
	L = luaL_newstate();
	lua_register(L,"myf",myf);
	luaL_openlibs(L);
	return 0;
}






#endif